package com.day09;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TablePanelFrame extends JFrame {

    private static final int WINDOW_WIDTH = 640;
    private static final int WINDOW_HEIGHT = 640;

    private FilmTableModel filmTableModel;
    private JSplitPane splitPane, splitPane2;
    private JTable table;
    private JScrollPane jsp;
    private JPanel panel, btnPanel;
    private JTextField jtf1, jtf2, jtf3;
    private JLabel jl1, jl2, jl3;
    private JButton addBtn, updateBtn, deleteBtn;

    private int index;

    public TablePanelFrame() {
        initComponent();
        addComponent();
        addListener();
        initWindows();
    }

    private void initComponent() {
        jtf1 = new JTextField(48);
        jtf2 = new JTextField(48);
        jtf3 = new JTextField(48);
        jl1 = new JLabel("电影ID");
        jl2 = new JLabel("电影名");
        jl3 = new JLabel("上映时间");
        addBtn = new JButton("新增");
        updateBtn = new JButton("修改");
        deleteBtn = new JButton("删除");

        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        panel = new JPanel();
        btnPanel = new JPanel();
        filmTableModel = new FilmTableModel();
        List<Film> list = new ArrayList<>();
        Film f1 = new Film(1, "让子弹飞", new Date());
        Film f2 = new Film(2, "让子弹飞", new Date());
        Film f3 = new Film(3, "让子弹飞", new Date());
        Film f4 = new Film(4, "让子弹飞", new Date());
        list.add(f1);
        list.add(f2);
        list.add(f3);
        list.add(f4);
        filmTableModel.setFilmList(list);
        table = new JTable(filmTableModel);
        jsp = new JScrollPane(table);
    }

    private void addComponent() {
        panel.add(jl1);
        panel.add(jtf1);
        panel.add(jl2);
        panel.add(jtf2);
        panel.add(jl3);
        panel.add(jtf3);
        btnPanel.add(addBtn);
        btnPanel.add(updateBtn);
        btnPanel.add(deleteBtn);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
        splitPane2.setTopComponent(panel);
        splitPane2.setBottomComponent(btnPanel);
        splitPane2.setDividerLocation(200);
        splitPane2.setDividerSize(2);
        splitPane.setTopComponent(jsp);
        splitPane.setBottomComponent(splitPane2);
        splitPane.setDividerLocation(300);
        splitPane.setDividerSize(2);
        this.add(splitPane);
    }

    private void addListener() {
        table.addMouseListener(
                new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        index = table.rowAtPoint(e.getPoint());
                        Film f = filmTableModel.getFilmByIndex(index);
                        jtf1.setText(String.valueOf(f.getFilmId()));
                        jtf2.setText(f.getFilmName());
                        jtf3.setText(String.valueOf(new SimpleDateFormat("yyyy-MM-dd").format(f.getFilmPublishTime())));
                    }
                }
        );
        addBtn.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String id = jtf1.getText();
                        String name = jtf2.getText();
                        String publishTime = jtf3.getText();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Film film = null;
                        try {
                            film = new Film(Integer.parseInt(id), name, sdf.parse(publishTime));
                        } catch (ParseException ex) {
                            ex.printStackTrace();
                        }
                        filmTableModel.addFilm(film);
                        jtf1.setText(null);
                        jtf2.setText(null);
                        jtf3.setText(null);
                        table.updateUI();
                    }
                }
        );
        updateBtn.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                    }
                }
        );
        deleteBtn.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                    }
                }
        );
    }

    private void initWindows() {
        this.setTitle("TablePanelFrame");
        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new TablePanelFrame();
    }

    class Film {
        private int filmId;
        private String filmName;
        private Date filmPublishTime;

        public Film() {
        }

        public Film(int filmId, String filmName, Date filmPublishTime) {
            this.filmId = filmId;
            this.filmName = filmName;
            this.filmPublishTime = filmPublishTime;
        }

        public int getFilmId() {
            return filmId;
        }

        public void setFilmId(int filmId) {
            this.filmId = filmId;
        }

        public String getFilmName() {
            return filmName;
        }

        public void setFilmName(String filmName) {
            this.filmName = filmName;
        }

        public Date getFilmPublishTime() {
            return filmPublishTime;
        }

        public void setFilmPublishTime(Date filmPublishTime) {
            this.filmPublishTime = filmPublishTime;
        }

        @Override
        public String toString() {
            return "Film{" +
                    "filmId=" + filmId +
                    ", filmName='" + filmName + '\'' +
                    ", filmPublishTime=" + filmPublishTime +
                    '}';
        }
    }

    class FilmTableModel extends AbstractTableModel {

        private final String[] header = {"编号", "电影名称", "上映时间"};
        private List<Film> filmList = null;

        public List<Film> getFilmList() {
            return filmList;
        }

        public void setFilmList(List<Film> filmList) {
            this.filmList = filmList;
        }

        @Override
        public String getColumnName(int column) {
            return header[column];
        }

        @Override
        public int getRowCount() {
            return filmList.size();
        }

        @Override
        public int getColumnCount() {
            return header.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Object result = null;
            Film film = filmList.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    result = film.getFilmId();
                    break;
                case 1:
                    result = film.getFilmName();
                    break;
                case 2:
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    result = simpleDateFormat.format(film.getFilmPublishTime());
                    break;

                default:
                    break;
            }
            return result;
        }

        public void addFilm(Film film) {
            filmList.add(film);
        }

        public void deleteFilm(int index) {
            filmList.remove(index);
        }

        public void updateFilm(int index, Film film) {
            filmList.set(index, film);
        }

        public Film getFilmByIndex(int index) {
            return filmList.get(index);
        }
    }
}
